﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using Domaine.Contracts;

namespace Application.Services
{
    public class AppMembershipService : IAppMembershipService
    {
        #region members

        IMemberShipRepository _memberShipRepository;
        IUserRepository _userRepository;
        IAspApplicationRepository _aspApplicationRepository;

        #endregion
        #region ctor
        public AppMembershipService(IMemberShipRepository memberShipRepository, IUserRepository userRepository,
            IAspApplicationRepository aspApplicationRepository
                                                    )
        {
            if (memberShipRepository == null)
                throw new ArgumentNullException("memberShipRepository");
            if (userRepository == null)
                throw new ArgumentNullException("userRepository");
            if (aspApplicationRepository == null)
                throw new ArgumentNullException("aspApplicationRepository");
            _aspApplicationRepository = aspApplicationRepository;
            _memberShipRepository = memberShipRepository;
            _userRepository = userRepository;
        }
        #endregion

        #region IAppMembershipService

        public Domaine.Entity.Membership GetMemmberByUserName(string username)
        {
            Domaine.Entity.Membership member = this._memberShipRepository.GetFilteredFullElements(m => m.User.UserName == username).FirstOrDefault();
            return member;
        }
       public Domaine.Entity.Membership GetMemmberByUserID(Guid id)
        {
            Domaine.Entity.Membership member = this._memberShipRepository.GetFullElemnt(id);
            return member;
        }
       public Domaine.Entity.Membership GetMemmberByUserEmail(string email)
       {
           Domaine.Entity.Membership member = this._memberShipRepository.GetFilteredFullElements(m => m.Email == email).FirstOrDefault();
           return member;
       }


       public void  AddMember(Domaine.Entity.Membership member)
       {
           this._memberShipRepository.Add(member);
           this._memberShipRepository.UnitOfWork.Commit();
       }


        public void UpdateMemmber(Domaine.Entity.Membership member)
        {
            this._memberShipRepository.Modify(member);
            this._memberShipRepository.UnitOfWork.Commit();
        }



        public IEnumerable<Domaine.Entity.Membership> GetPagedMembers(int pageIndex, int pageSize)
        {
            return this._memberShipRepository.GetPagedFullElements(pageIndex, pageSize );
        }


        public void RemoveMember(Domaine.Entity.Membership member)
        {
            this._memberShipRepository.Remove(member);
            this._memberShipRepository.UnitOfWork.Commit();
        }









       public Domaine.Entity.AspApplication GetAspApplication()
        {
            return this._aspApplicationRepository.GetAll().FirstOrDefault();
        }

       public void AddAspApplication(Domaine.Entity.AspApplication app)
       {
           this._aspApplicationRepository.Add(app);
           this._aspApplicationRepository.UnitOfWork.Commit();
       }
        #endregion



       
    }
}
